package com.xian.im.web.modules.user;

import com.xian.im.common.model.from.UserFrom;
import com.xian.im.common.model.vo.UserVo;
import com.xian.im.common.result.RestResult;
import com.xian.im.common.result.RestResultBuilder;
import com.xian.im.common.validator.ValidatorUtils;
import com.xian.im.common.validator.group.*;
import com.xian.im.modules.avchat.manager.ImUserManager;
import com.xian.im.modules.im.core.IUserCoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 
 *
 * @author xlr
 * @email 
 * @date 2021-01-18 19:11:56
 */
@RestController
@RequestMapping("im/user")
@Api(value = "用户管理API", tags = {"用户管理"})
public class UserController {

    @Autowired
    private IUserCoreService userCoreService;
    @Autowired
    private ImUserManager userManager;

    @ApiOperation(value = "添加用户", notes = "添加用户")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public RestResult<UserVo> login(@RequestBody UserFrom from){
        ValidatorUtils.validateEntity(from, AddGroup.class);
        UserVo user = userCoreService.createUser(from);
        return RestResultBuilder.builder().success().data(user).build();
    }
    @ApiOperation(value = "修改用户信息", notes = "修改用户信息")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public RestResult<Boolean> update(@RequestBody UserFrom from){
        ValidatorUtils.validateEntity(from, UpdateGroup.class);
        Boolean aBoolean = userCoreService.updateUserInfo(from);
        return RestResultBuilder.builder().success().data(aBoolean).build();
    }
    @ApiOperation(value = "修改用户密码", notes = "修改用户密码")
    @RequestMapping(value = "/update/password",method = RequestMethod.POST)
    public RestResult<Boolean> updatePassword(@RequestBody UserFrom from){
        ValidatorUtils.validateEntity(from, CheckGroup.class);
        Boolean aBoolean = userCoreService.updatePassword(from);
        return RestResultBuilder.builder().success().data(aBoolean).build();
    }
    @ApiOperation(value = "发送系统点对点消息", notes = "发送系统点对点消息")
    @RequestMapping(value = "/sendMessage",method = RequestMethod.GET)
    public RestResult<Boolean> sendMessage(@RequestParam("body")String body, @RequestParam("account")String account){
        userManager.sendSystemMessage(body,account);
        return RestResultBuilder.builder().success().data(Boolean.TRUE).build();
    }

}
